﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

using KELT;
using KELT.Animation;


namespace KELT.KELTObjects
{
    public class SortableObservableCollection<t> : ObservableCollection<t>
    {
        public void Sort<tkey>(Func<t, tkey> keySelector, System.ComponentModel.ListSortDirection direction)
        {
            switch (direction)
            {
                case System.ComponentModel.ListSortDirection.Ascending:
                    {
                        ApplySort(Items.OrderBy(keySelector));
                        break;
                    }
                case System.ComponentModel.ListSortDirection.Descending:
                    {
                        ApplySort(Items.OrderByDescending(keySelector));
                        break;
                    }
            }
        }

        public void Sort<tkey>(Func<t, tkey> keySelector, IComparer<tkey> comparer)
        {
            ApplySort(Items.OrderBy(keySelector, comparer));
        }

        private void ApplySort(IEnumerable<t> sortedItems)
        {
            var sortedItemsList = sortedItems.ToList();

            foreach (var item in sortedItemsList)
            {
                Move(IndexOf(item), sortedItemsList.IndexOf(item));
            }
        }
    }
    
    public class AnimationCollection : SortableObservableCollection<IAnimation>
    {
        public AnimationCollection()
        {
            this.Sort(x => x.startTime, System.ComponentModel.ListSortDirection.Ascending);
        }
    }
    
    public class PageCollection : SortableObservableCollection<Page>
    {
        public PageCollection()
        {
            this.Sort(x => x.startTime, System.ComponentModel.ListSortDirection.Ascending);
        }
    }
    /*
    public class AnimationKeyFrameCollection : SortableObservableCollection<AnimationKeyFrame>
    {
        public AnimationKeyFrameCollection()
        {
            this.Sort(x => x.time, System.ComponentModel.ListSortDirection.Ascending);
        }
    }
     * */
}
